#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef DEBUG
#define DEBUG_PRINT(fmt, args...)    fprintf(stderr, fmt, ## args)
#else
#define DEBUG_PRINT(fmt, args...)    /* Don't do anything in release builds */
#endif


#define USER_LINE_MAX 100

// Global Variables
static FILE * pFile;
static char pcfName[100];
static char inputBuffer[USER_LINE_MAX];
char * PCDHeader = "# .PCD v.7 - Point Cloud Data file format\nVERSION .7\nFIELDS x y z\nSIZE 4 4 4\nTYPE F F F\nCOUNT 1 1 1\nVIEWPOINT 0 0 0 1 0 0 0\nDATA ascii\n";


void welcome()
{
	printf("\nLaser Scanning Instrument User Program\n");
	printf("Version: 1.0\n\n");
	printf("Welcome!\n");
	printf("(Instructions here)\n\n");

}

void createPCF()
{
	// Accept User Input
	printf("Please enter the name of the file (do not enter extension): ");
	fflush(stdout);

	// Validate and create file
	if(fgets(inputBuffer, (USER_LINE_MAX - 4), stdin) == NULL)
	{
		printf("Name must be at least 1 character, exiting program...");
		exit(EXIT_FAILURE);
	}

	sscanf(inputBuffer, "%s", pcfName);
	strcat(pcfName, ".pcd");

	pFile = fopen(pcfName, "w");

	if(pFile == NULL)
	{
		printf("File pointer is NULL, exiting program...");
		exit(EXIT_FAILURE);
	}

	fprintf(pFile, PCDHeader);

}

void genScanningRoute();
void sendRoute();

void receiveCoordinates()
{
	
}

void shutdownProgram()
{
	// Close point cloud file
	fclose(pFile);
}

int main()
{
	// Welcome and Instructions
	welcome();

	// Create Point Cloud File
	createPCF();

	// Accept Scanning Route from User

	// Send user-inputted scanning route information to microcontroller

	// Receive X,Y,Z coordinates back from microcontroller and store in point cloud
	receiveCoordinates();
	// Complete mission and exit
	shutdownProgram();

	exit(EXIT_SUCCESS);
}